import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Home from '../components/Home.vue'
import Menu from '../components/Menu.vue'
import About from '../components/about/About.vue'
import Register from '../components/Register.vue'
import Login from '../components/Login.vue'
import Admin from '../components/Admin.vue'

//二级路由
import Contact from '../components/about/Contact.vue'
import Delivery from '../components/about/Delivery.vue'
import History from '../components/about/History.vue'
import OrderingGuider from '../components/about/OrderingGuide.vue'
//三级路由
import Phone from '../components/about/contact/Phone.vue'
import personName from '../components/about/contact/PersonName.vue'
const routes=[
	// {path:'/',redirect:'/home'},
	//路由的复用
	{path:'/home',name:"homeLink",components:{
		default:Home,
		'orderingGuider':OrderingGuider,
		'delivery':Delivery,
		'history':History
	}},
	{path:'/menu',component:Menu},
	{path:'/about',component:About,redirect:'/about/contact',children:[
		{path:'contact',name:"contactLink",redirect:'/about/contact/personName',component:Contact,children:[
			{path:'phone',name:'phoneNum',component:Phone},
			{path:'personName',name:'personName',component:personName}
		]},
		{path:'history',name:'historyLink',component:History},
		{path:'Delivery',name:'deliveryLink',component:Delivery},
		{path:'OrderingGuider',name:'orderingGuiderLink',component:OrderingGuider}
	]},
	{path:'/Register',component:Register},
	{path:'/admin',component:Admin},
	{path:'/login',component:Login},
	//当输入一个不存在的地址的时候 自动跳转到/home页面
	{path:'*',redirect:'/home'}
]
const router =new VueRouter({
	routes,
	// 滚动行为 在路由改变之后 其中saveedPosition参数是重新回退会回到之前页面滚动到的位置
	// 也可以自定义回到其他位置 这个可以用在点击返回按钮的时候 路由停在之前浏览的位置
	scrollBehavior(to,from,savedPosition) {
		// return {selector:'.btn'}
		if(savedPosition){
			return savedPosition
		}else{
			return {x:0,y:0}
		}
	},
	mode:'history'
})
//全局守卫 判断页面是否跳转 进入任何路由会调用 只有执行next才会跳转
router.beforeEach((to,from,next)=>{
	
	
	if(to.path=='/login'||to.path=='/register'){
		next()
	}else if(window.localStorage.getItem("Login")!==null){
		next()
	}	else{
		if(window.localStorage.getItem("Login")!==null)
		window.localStorage.clear("Login")
		next('/login')
	}
})
//后置钩子 当已经跳转之后触发
// router.afterEach((to,from)=>{
// 	alert("after")
// })

export default router
